<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <div class="fixed-table-actionbar">
        <TableFilter />
        <ActionBar v-role="['workerGroupLeader']" />
      </div>
      <PageableTable
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        class="fixed-table-table"
        @page-change="onPageChange"
        @sort-change="onSortChange"
      >
        <el-table-column v-for="column in insideColumns" :key="column.key" :label="column.title" :width="column.width" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <!-- <span v-html="brightenKeyword(scope.row.strContenet,filters.strContent)" /> -->
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button v-if="row.disabled" v-role="['workerGroupLeader']" title="编辑" type="text" size="mini" icon="el-icon-edit" @click="edit(row.id)" />
            <el-button v-if="row.disabled" v-role="['workerGroupLeader']" title="删除" type="text" size="mini" icon="el-icon-delete" @click="remove(row.id)" />
            <el-button v-role="['workerGroupLeader']" :title="row.disabled===true ?'启动':'停止'" type="text" size="mini" :icon="row.disabled===true ?'el-icon-video-play':'el-icon-video-pause'" @click="enable(row)" />
          </template>
        </el-table-column>
      </PageableTable>
    </div>
    <FormDialog title="编辑" class="fixed-dialog-height" :show.sync="dialogFormVisible" width="70%" @on-ok="ok" @on-cancel="cancel">
      <EditForm v-if="dialogFormVisible" :id="editId" ref="editForm" />
    </FormDialog>
  </div>
</template>

<script>
// secondary package based on el-pagination
// import SelectDialog from '@neu/biz-core/components/SelectDialog'
import FormDialog from '@neu/biz-core/components/FormDialog'
import TableFilter from './components/tableFilter'
import EditForm from './components/editForm'
import ActionBar from './components/actionBar'
import PageableTable from '@neu/biz-core/components/PageableTable'
import databind from '@neu/biz-core/mixins/databind'
import { formatDate } from '@neu/biz-core/utils/utils'
import { page, remove, enable, disable } from '@neu/dcom-ui/api/hnInspectionPlan'
export default {
  name: 'InspectionItem',
  components: {
    TableFilter,
    FormDialog,
    EditForm,
    ActionBar,
    PageableTable
    // SelectDialog
  },
  // 加入混入，使表格混入更多的api操作
  mixins: [databind],
  props: {

  },
  data() {
    return {
      r: page,
      d: remove,
      // 巡检类型
      types: [],
      // 数据类型
      dataTypes: [],
      insideColumns: [
        { title: '厅局', key: 'institutionName', sortable: 'custom', display: true },
        { title: '开始时间', key: 'startDateTime', sortable: 'custom', render: (h, params) => { return h('span', formatDate(params.row.startDateTime, 'YYYY-MM-DD')) }, display: true },
        { title: '结束时间', key: 'endDateTime', sortable: 'custom', render: (h, params) => { return h('span', formatDate(params.row.endDateTime, 'YYYY-MM-DD')) }, display: true },
        { title: '巡检时长', key: 'duration', sortable: 'custom', display: true },
        { title: '计划状态', key: 'disabled', sortable: 'custom', display: true, render: (h, params) => {
          if (!params.row.disabled) {
            return h('span', [
              h('el-tag', '启用')
            ])
          } else {
            return h('span', [
              h('el-tag', {
                props: {
                  type: 'info'
                }
              }, '禁用')
            ])
          }
        } },
        { title: '维护人', key: 'userName', sortable: 'custom', display: true },
        { title: '维护时间', key: 'updateTime', sortable: 'custom', render: (h, params) => { return h('span', formatDate(params.row.updateTime)) }, display: true }
      ],
      dialogFormVisible: false,
      dialogStatus: 'create',
      editId: -1
    }
  },
  methods: {
    // 表格编辑按钮点击事件处理
    edit(id) {
      const roles = this.$store.getters.roles
      if (roles && !roles.includes('workerGroupLeader')) {
        return this.$message({
          showClose: true,
          message: '只有一线运维组长可以编辑日巡检计划',
          type: 'error'
        })
      }
      // 打开对话框
      this.editId = id
      this.dialogFormVisible = true
      // 更新当前被编辑实体的ID，自动传递至编辑表单
    },
    // 编辑资产对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
      })
    },
    cancel() {
      this.editId = -1
      this.$refs.editForm.reset()
      // 关闭对话框
      this.dialogFormVisible = false
    },
    enable(row) {
      let api = enable
      if (row.disabled === true) {
        api = enable
      } else {
        api = disable
      }
      api(row.id).then(res => {
        if (res.status === 'success') {
          this.$message({
            showClose: true,
            message: '更新状态成功',
            type: 'success'
          })
          this.refresh()
        } else {
          this.$message({
            showClose: true,
            message: '更新状态失败',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
